<?php
namespace app\shop\model;

use think\Db;

class Report extends \ShopModel
{
    protected $table = 'sh_crm_report';

    public function search($params){

        $where = [];
        $client_report_id = [];
        $staff_report_id = [];
        $type_report_id = [];

        if(!empty($params['client_id'])){
            $table = Db::name('crm_report_client_relevance')->alias('a')
            ->join('crm_report b','a.report_id = b.report_id')
            ->where('a.client_id',$params['client_id'])
            ->field('b.*')
            ->buildSql();
        }

        if(!empty($params['staff_id'])){
            $table = Db::name('crm_report_staff_relevance')->alias('a')
            ->join('crm_report b','a.report_id = b.report_id')
            ->where('a.staff_id',$params['staff_id'])
            ->field('b.*')
            ->buildSql();
        }

        if(!empty($params['type'])){
            $report_ids = Db::name('crm_report_type_relevance')->where('type_id',$params['type'])->column('report_id');
            $where['report_id'] = ['in',$report_ids];
        }

        if(!empty($params['create_staff'])){
            $where['create_staff'] = ['like','%'.$params['create_staff'].'%'];
        }

        if(!empty($params['create_at'])){
            $where['create_at'] = ['between',$params['create_at']];
        }

        //权限
        $report_access = $this->report_role();

        if(!$report_access['is_admin']){
            $where['create_staff_id'] = ['in',$report_access['staff_ids']];
        }

        //主数据
        $data = Db::table($table)->alias('report')->where($where)->select();

        foreach($data as &$v){
            //抄送
            $staff_id_arr = Db::name('report_cc_staff')->where('report_id',$v['report_id'])->column('staff_id');
            $final_staff = [];
            if(!empty($staff_id_arr)){
                foreach($staff_id_arr as $staff_id){
                    $tmp_staff = Db::name('crm_staff')->where('staff_id',$staff_id)->value('staff_name');
                    $final_staff[] = [
                        'staff_id' => $staff_id,
                        'staff_name' => $tmp_staff
                    ];
                }
            }

            $v['cc_staff'] = $final_staff;
            $department_id_arr = Db::name('report_cc_department')->where('report_id',$v['report_id'])->column('department_id');
            $final_department = [];
            if(!empty($department_id_arr)){
                foreach($department_id_arr as $department_id){
                    $tmp_department = Db::name('crm_department')->where('department_id',$department_id)->value('department_name');
                    $final_department[] = [
                        'department_id' => $department_id,
                        'department_name' => $tmp_department
                    ];
                }  
            }
            $v['cc_department'] = $final_department;

            //点赞
            $v['likes'] = Db::name('crm_report_likes')->field('staff_id,staff_name')->where('report_id',$v['report_id'])->select();
            //评论
            $v['comment'] = Db::name('crm_report_comment')->where('report_id',$v['report_id'])->where('parent_id',0)
            ->order('create_at asc')->select();
            foreach($v['comment'] as &$c){
                $c['is_show'] = false;
                $c['child_comment'] = Db::name('crm_report_comment')->order('create_at asc')->where('parent_id',$c['comment_id'])->select();
                foreach($c['child_comment'] as &$cc){
                    $cc['is_show'] = false;
                }
            }
            //标签
            $v['type'] = Db::name('crm_report_type_relevance')->alias('a')
            ->join('crm_report_type b','a.type_id = b.type_id')
            ->where('a.report_id',$v['report_id'])
            ->column('b.title');

            //关联联系人
            if(!empty($params['staff_id'])){
                $v['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
                ->join('sh_crm_staff b','a.staff_id = b.staff_id')
                ->where('a.report_id',$v['report_id'])
                ->where('a.staff_id','<>',$params['staff_id'])
                ->field('b.staff_id,staff_name')
                ->select();
            }else{
                $v['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
                ->join('sh_crm_staff b','a.staff_id = b.staff_id')
                ->where('a.report_id',$v['report_id'])
                ->field('b.staff_id,staff_name')
                ->select();
            }
            
            //关联客户
            if(!empty($params['client_id'])){
                $v['client'] = Db::name('crm_report_client_relevance')->alias('a')
                ->join('sh_client b','a.client_id = b.client_id')
                ->where('a.report_id',$v['report_id'])
                ->where('a.client_id','<>',$params['client_id'])
                ->field('b.client_id,client_name')
                ->select();
            }else{
                $v['client'] = Db::name('crm_report_client_relevance')->alias('a')
                ->join('sh_client b','a.client_id = b.client_id')
                ->where('a.report_id',$v['report_id'])
                ->field('b.client_id,client_name')
                ->select();
            }

            //图片
            $v['image'] = Db::name('crm_report_img_relevance')->alias('a')
            ->join('sh_img b','a.img_id = b.img_id')
            ->where('a.report_id',$v['report_id'])->column('b.img_url');
            //附件
            $v['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
            ->join('sh_accessory b','a.accessory_id = b.accessory_id')
            ->where('a.report_id',$v['report_id'])->field('b.accessory_id,b.accessory_name,b.accessory_url')->select();

        }

        return $data;

    }

    //获取汇报权限
    public function report_role(){

        $utid = \request()->header('userId');

        $is_sys_admin = Db::name('crm_staff')->where('staff_Id',$utid)->value('sys_custodian');

        if(!empty($is_sys_admin)){
            return [
                'is_admin' => 1
            ];
        }

        $is_admin = Db::name('crm_report_staff')->where(["staff_id"=>$utid,"is_manager"=>1])->find();

        if(!empty($is_admin)){
            return [
                'is_admin' => 1
            ];
        }

        $access = Db::name('crm_report_check_role')->where(["staff_id"=>$utid])->find();

        $staff_ids = [];

        $self = [$utid];
        $tmp_s = [];
        $tmp_d = [];

        if($access){
            if(!empty($access['staff_list'])){
                $tmp_s = explode(',',$access['staff_list']);
            }

            if(!empty($access['depart_list'])){
                $dep_list = explode(',',$access['depart_list']);
                $tmp_d = Db::name('crm_staff')->where('is_downstream',1)->where('department_id','in',$dep_list);
            }
        }

        $staff_ids = array_unique(array_merge($tmp_s,$tmp_d,$self));

        return [
            'is_admin' => 0,
            'staff_ids' => $staff_ids
        ];

    }

    public function add($params){
        $utid = \request()->header('userId');
        $staff = Db::name('crm_staff')->alias('a')
        ->join('sh_crm_department b','a.department_id = b.department_id')
        ->where('a.staff_id',$utid)->field('a.staff_id,a.staff_name,b.department_name')->find();

        //主数据
        $report = [];
        $report['report_content'] = $params['report_content'];
        $report['report_action'] = $params['report_action'];
        $report['date_begin'] = !empty($params['date_begin']) ? $params['date_begin'] : null;
        $report['date_end'] = !empty($params['date_end']) ? $params['date_end'] : null;
        $report['only_mine'] = $params['only_mine'] === 'true' ? 1 : 0;
        $report['sync_company'] = $params['sync_company'] === 'true' ? 1 : 0;
        $report['create_staff_id'] = !empty($staff['staff_id']) ? $staff['staff_id'] : null; 
        $report['create_staff'] = !empty($staff['staff_name']) ? $staff['staff_name'] : null; 
        $report['create_at'] = date('Y-m-d H:i:s');
        $report['department'] = !empty($staff['department_name']) ? $staff['department_name'] : null; 

        $report_id = Db::name('crm_report')->insertGetId($report);

        //抄送
        if(!empty($params['cc_staff'])){
            foreach($params['cc_staff'] as $cc_staff){
                Db::name('report_cc_staff')->insert(['report_id'=>$report_id,'staff_id'=>$cc_staff]);
            }
        }

        if(!empty($params['cc_department'])){
            foreach($params['cc_department'] as $cc_department){
                Db::name('report_cc_department')->insert(['report_id'=>$report_id,'department_id'=>$cc_department]);
            }
        }

        //标签
        if(!empty($params['type'])){
            foreach($params['type'] as $type_id){
                $report_type = [
                    'report_id' => $report_id,
                    'type_id' => $type_id
                ];
                Db::name('crm_report_type_relevance')->insert($report_type);
            }
        }

        //关联客户
        if(!empty($params['client'])){
            foreach($params['client'] as $client){
                $report_client = [
                    'report_id' => $report_id,
                    'client_id' => $client['client_id']
                ];
                Db::name('crm_report_client_relevance')->insert($report_client);
            }
        }

        //关联联系人
        if(!empty($params['staff'])){
            foreach($params['staff'] as $staff){
                $report_staff = [
                    'report_id' => $report_id,
                    'staff_id' => $staff['staff_id']
                ];
                Db::name('crm_report_staff_relevance')->insert($report_staff);
            }
        }

        //图片
        if(!empty($params['image'])){
            foreach($params['image'] as $img){
                $img_data = [
                    'img_url' => $img
                ];
                $img_id = Db::name('img')->insertGetId($img_data);
                $report_img = [
                    'report_id' => $report_id,
                    'img_id' => $img_id
                ];
                Db::name('crm_report_img_relevance')->insert($report_img);
            }
        }

        //附件
        if(!empty($params['accessory'])){
            foreach($params['accessory'] as $accessory){
                $tmp = explode('|',$accessory);
                $accessory_data = [
                    'accessory_name' => $tmp[1],
                    'accessory_url' => $tmp[0]
                ];
                $accessory_id = Db::name('accessory')->insertGetId($accessory_data);
                $report_accessory = [
                    'report_id' => $report_id,
                    'accessory_id' => $accessory_id
                ];
                Db::name('crm_report_accessory_relevance')->insert($report_accessory);
            }
        }

        return true;

    }

    public function detail($id){
        //主数据
        $data = Db::name('crm_report')->where('report_id',$id)->find();

        //抄送
        $data['cc_staff'] = Db::name('report_cc_staff')->where('report_id',$id)->column('staff_id');
        $data['cc_department'] = Db::name('report_cc_department')->where('report_id',$id)->column('department_id');

        //标签
        $data['type'] = Db::name('crm_report_type_relevance')->alias('a')
        ->join('sh_crm_report_type b','a.type_id = b.type_id')
        ->where('a.report_id',$id)
        ->column('b.type_id');

        //图片
        $data['image'] = Db::name('crm_report_img_relevance')->alias('a')
        ->join('sh_img b','a.img_id = b.img_id')
        ->where('a.report_id',$id)
        ->column('b.img_url');

        //附件
        $data['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
        ->join('sh_accessory b','a.accessory_id = b.accessory_id')
        ->where('a.report_id',$id)
        ->field('b.accessory_name name,b.accessory_url url')->select();

        foreach($data['accessory'] as &$a){
            $a['_t'] = $a['url'].'|'.$a['name'];
        };

        //关联联系人
        $data['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
        ->join('sh_crm_staff b','a.staff_id = b.staff_id')
        ->where('a.report_id',$id)
        ->field('b.staff_id,staff_name')
        ->select();
        
        //关联客户
        $data['client'] = Db::name('crm_report_client_relevance')->alias('a')
        ->join('sh_client b','a.client_id = b.client_id')
        ->field('b.client_id,client_name')
        ->where('a.report_id',$id)
        ->select();

        return $data;

    }

    public function edit($params){
        
        if(!empty($params['type'])){
            $type = $params['type'];
            unset($params['type']);
        }

        if(!empty($params['image'])){
            $image = $params['image'];
            unset($params['image']);
        }

        if(!empty($params['accessory'])){
            $accessory = $params['accessory'];
            unset($params['accessory']);
        }

        if(!empty($params['client'])){
            $client = $params['client'];
            unset($params['client']);
        }

        if(!empty($params['staff'])){
            $staff = $params['staff'];
            unset($params['staff']);
        }

        if(!empty($params['cc_staff'])){
            $cc_staff = $params['cc_staff'];
            unset($params['cc_staff']);
        }

        if(!empty($params['cc_department'])){
            $cc_department = $params['cc_department'];
            unset($params['cc_department']);
        }

        $report_id = $params['report_id'];

        $params['date_begin'] = !empty($params['date_begin']) ? $params['date_begin'] : null;
        $params['date_end'] = !empty($params['date_end']) ? $params['date_end'] : null;

        Db::name('crm_report')->where('report_id',$report_id)->update($params);
        
        //抄送
        Db::name('report_cc_staff')->where('report_id',$report_id)->delete();
        if(!empty($cc_staff)){
            foreach($cc_staff as $cc_staff_id){
                Db::name('report_cc_staff')->insert(['report_id'=>$report_id,'staff_id'=>$cc_staff_id]);
            }
        }

        Db::name('report_cc_department')->where('report_id',$report_id)->delete();
        if(!empty($cc_department)){
            foreach($cc_department as $cc_department_id){
                Db::name('report_cc_department')->insert(['report_id'=>$report_id,'department_id'=>$cc_department_id]);
            }
        }

        //标签
        Db::name('crm_report_type_relevance')->where('report_id',$report_id)->delete();
        if(!empty($type)){
            foreach($type as $type_id){
                $report_type = [
                    'report_id' => $report_id,
                    'type_id' => $type_id
                ];
                Db::name('crm_report_type_relevance')->insert($report_type);
            }
        }

        //关联客户
        Db::name('crm_report_client_relevance')->where('report_id',$report_id)->delete();
        if(!empty($client)){
            foreach($client as $c){
                $report_client = [
                    'report_id' => $report_id,
                    'client_id' => $c['client_id']
                ];
                Db::name('crm_report_client_relevance')->insert($report_client);
            }
        }

        //关联联系人
        Db::name('crm_report_staff_relevance')->where('report_id',$report_id)->delete();
        if(!empty($staff)){
            foreach($staff as $s){
                $report_staff = [
                    'report_id' => $report_id,
                    'staff_id' => $s['staff_id']
                ];
                Db::name('crm_report_staff_relevance')->insert($report_staff);
            }
        }

        //图片
        Db::name('crm_report_img_relevance')->where('report_id',$report_id)->delete();
        if(!empty($image)){
            foreach($image as $i){
                $img_data = [
                    'img_url' => $i
                ];
                $img_id = Db::name('img')->insertGetId($img_data);
                $report_img = [
                    'report_id' => $report_id,
                    'img_id' => $img_id
                ];
                Db::name('crm_report_img_relevance')->insert($report_img);
            }
        }

        //附件
        Db::name('crm_report_accessory_relevance')->where('report_id',$report_id)->delete();
        if(!empty($accessory)){
            foreach($accessory as $a){
                $tmp = explode('|',$a);
                $accessory_data = [
                    'accessory_name' => $tmp[1],
                    'accessory_url' => $tmp[0]
                ];
                $accessory_id = Db::name('accessory')->insertGetId($accessory_data);
                $report_accessory = [
                    'report_id' => $report_id,
                    'accessory_id' => $accessory_id
                ];
                Db::name('crm_report_accessory_relevance')->insert($report_accessory);
            }
        }

        return true;

    }

    //删除汇报
    public function del_report($id){

        //主数据
        Db::name('crm_report')->where('report_id',$id)->delete();

        //抄送
        Db::name('report_cc_staff')->where('report_id',$id)->delete();
        Db::name('report_cc_department')->where('report_id',$id)->delete();

        //标签
        Db::name('crm_report_type_relevance')->where('report_id',$id)->delete();

        //关联客户
        Db::name('crm_report_client_relevance')->where('report_id',$id)->delete();

        //关联联系人
        Db::name('crm_report_staff_relevance')->where('report_id',$id)->delete();

        //图片
        Db::name('crm_report_img_relevance')->where('report_id',$id)->delete();

        //附件
        Db::name('crm_report_accessory_relevance')->where('report_id',$id)->delete();

        //点赞
        Db::name('crm_report_likes')->where('report_id',$id)->delete();

        //评论
        Db::name('crm_report_comment')->where('report_id',$id)->delete();

        return true;
    }

    //删除评论
    public function del_comment($id){
        Db::name('crm_report_comment')->where('parent_id',$id)->delete();
        Db::name('crm_report_comment')->where('comment_id',$id)->delete();
        return true;
    }

    //点赞  
    public function likes($id){

        $utid = \request()->header('userId');
        $staff = Db::name('crm_staff')->where('staff_id',$utid)->value('staff_name');

        $flag = Db::name('crm_report_likes')->where('report_id',$id)->where('staff_id',$utid)->value('likes_id');

        if($flag){
            Db::name('crm_report_likes')->where('likes_id',$flag)->delete();
        }else{
            $likes = [
                'report_id' => $id,
                'staff_id' => $utid,
                'staff_name' => $staff
            ];
            Db::name('crm_report_likes')->insert($likes);
        }

        return true;

    }

    public function comment($params){
        $utid = \request()->header('userId');
        $staff = Db::name('crm_staff')->where('staff_id',$utid)->value('staff_name');

        $params['create_at'] =date('Y-m-d H:i:s');
        $params['staff_id'] = $utid;
        $params['staff_name'] = $staff;

        Db::name('crm_report_comment')->insert($params);

        return true;
    }

    public function removeClient($params){
        Db::name('crm_report_client_relevance')->where('report_id',$params['report_id'])->where('client_id',$params['client_id'])->delete();
        return true;
    }

    public function removeStaff($params){
        Db::name('crm_report_staff_relevance')->where('report_id',$params['report_id'])->where('staff_id',$params['staff_id'])->delete();
        return true;
    }

    public function removeImage($params){
        Db::name('crm_report_img_relevance')->where('report_id',$params['report_id'])->where('img_id',$params['img_id'])->delete();
        return true;
    }

    public function removeAccessory($params){
        Db::name('crm_report_accessory_relevance')->where('report_id',$params['report_id'])->where('accessory_id',$params['accessory_id'])->delete();
        return true;
    }

    public function type(){
        return Db::name('crm_report_type')->field('type_id,title')->where('is_delete',0)->select();
    }

    public function changeState($params){
        Db::name('crm_report')->where('report_id',$params['report_id'])->update(['report_state'=>$params['report_state']]);
        return true;
    }


    /**
     * 手机端接口
     */

    //我的汇报
    public function my_report($params){

        $utid = \request()->header('userId');

        $where = [];

        //权限
        $report_access = $this->report_role();

        if(!$report_access['is_admin']){
            $where['create_staff_id'] = ['in',$report_access['staff_ids']];
        }

        if(!empty($params['date'])){
            $where['DATE(create_at)'] = $params['date'];
        }

        //主数据
        $report = Db::name('crm_report')->where('create_staff_id',$utid)->where($where)->select();

        foreach($report as &$v){

            //抄送
            $staff_id_arr = Db::name('report_cc_staff')->where('report_id',$v['report_id'])->column('staff_id');
            $final_staff = [];
            if(!empty($staff_id_arr)){
                foreach($staff_id_arr as $staff_id){
                    $tmp_staff = Db::name('crm_staff')->where('staff_id',$staff_id)->value('staff_name');
                    $final_staff[] = [
                        'staff_id' => $staff_id,
                        'staff_name' => $tmp_staff
                    ];
                }
            }

            $v['cc_staff'] = $final_staff;
            $department_id_arr = Db::name('report_cc_department')->where('report_id',$v['report_id'])->column('department_id');
            $final_department = [];
            if(!empty($department_id_arr)){
                foreach($department_id_arr as $department_id){
                    $tmp_department = Db::name('crm_department')->where('department_id',$department_id)->value('department_name');
                    $final_department[] = [
                        'department_id' => $department_id,
                        'department_name' => $tmp_department
                    ];
                }  
            }
            $v['cc_department'] = $final_department;

            //点赞
            $v['likes'] = Db::name('crm_report_likes')->field('staff_id,staff_name')->where('report_id',$v['report_id'])->count();
            //评论
            $v['comment'] = Db::name('crm_report_comment')->where('report_id',$v['report_id'])->count();
            //标签
            $v['type'] = Db::name('crm_report_type_relevance')->alias('a')
            ->join('crm_report_type b','a.type_id = b.type_id')
            ->where('a.report_id',$v['report_id'])
            ->column('b.title');

            //关联联系人
            $v['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
            ->join('sh_crm_staff b','a.staff_id = b.staff_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.staff_id,staff_name')
            ->select();
            
            //关联客户
            $v['client'] = Db::name('crm_report_client_relevance')->alias('a')
            ->join('sh_client b','a.client_id = b.client_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.client_id,client_name')
            ->select();

            //图片
            $v['image'] = Db::name('crm_report_img_relevance')->alias('a')
            ->join('sh_img b','a.img_id = b.img_id')
            ->where('a.report_id',$v['report_id'])->column('b.img_url');
            //附件
            $v['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
            ->join('sh_accessory b','a.accessory_id = b.accessory_id')
            ->where('a.report_id',$v['report_id'])->field('b.accessory_id,b.accessory_name,b.accessory_url')->select();

        }

        return $report;

    }

    //抄送我的汇报
    public function cc_mine_report($params){

        $utid = \request()->header('userId');

        $where = [];

        //权限
        $report_access = $this->report_role();

        if(!$report_access['is_admin']){
            $where['create_staff_id'] = ['in',$report_access['staff_ids']];
        }

        $report_ids = Db::name('report_cc_staff')->where('staff_id',$utid)->column('report_id');

        $where['report_id'] = ['in',$report_ids];

        if(!empty($params['date'])){
            $where['DATE(create_at)'] = $params['date'];
        }

        //主数据
        $report = Db::name('crm_report')->where($where)->select();

        foreach($report as &$v){

            //抄送
            $staff_id_arr = Db::name('report_cc_staff')->where('report_id',$v['report_id'])->column('staff_id');
            $final_staff = [];
            if(!empty($staff_id_arr)){
                foreach($staff_id_arr as $staff_id){
                    $tmp_staff = Db::name('crm_staff')->where('staff_id',$staff_id)->value('staff_name');
                    $final_staff[] = [
                        'staff_id' => $staff_id,
                        'staff_name' => $tmp_staff
                    ];
                }
            }

            $v['cc_staff'] = $final_staff;
            $department_id_arr = Db::name('report_cc_department')->where('report_id',$v['report_id'])->column('department_id');
            $final_department = [];
            if(!empty($department_id_arr)){
                foreach($department_id_arr as $department_id){
                    $tmp_department = Db::name('crm_department')->where('department_id',$department_id)->value('department_name');
                    $final_department[] = [
                        'department_id' => $department_id,
                        'department_name' => $tmp_department
                    ];
                }  
            }
            $v['cc_department'] = $final_department;

            //点赞
            $v['likes'] = Db::name('crm_report_likes')->field('staff_id,staff_name')->where('report_id',$v['report_id'])->count();
            //评论
            $v['comment'] = Db::name('crm_report_comment')->where('report_id',$v['report_id'])->count();
            //标签
            $v['type'] = Db::name('crm_report_type_relevance')->alias('a')
            ->join('crm_report_type b','a.type_id = b.type_id')
            ->where('a.report_id',$v['report_id'])
            ->column('b.title');

            //关联联系人
            $v['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
            ->join('sh_crm_staff b','a.staff_id = b.staff_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.staff_id,staff_name')
            ->select();
            
            //关联客户
            $v['client'] = Db::name('crm_report_client_relevance')->alias('a')
            ->join('sh_client b','a.client_id = b.client_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.client_id,client_name')
            ->select();

            //图片
            $v['image'] = Db::name('crm_report_img_relevance')->alias('a')
            ->join('sh_img b','a.img_id = b.img_id')
            ->where('a.report_id',$v['report_id'])->column('b.img_url');
            //附件
            $v['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
            ->join('sh_accessory b','a.accessory_id = b.accessory_id')
            ->where('a.report_id',$v['report_id'])->field('b.accessory_id,b.accessory_name,b.accessory_url')->select();

        }

        return $report;

    } 

    //他人汇报
    public function other_report($params){

        $utid = \request()->header('userId');

        $where = [];

        //权限
        $report_access = $this->report_role();

        if(!$report_access['is_admin']){
            $where['create_staff_id'] = ['in',$report_access['staff_ids']];
        }

        if(!empty($params['date'])){
            $where['DATE(create_at)'] = $params['date'];
        }

        //主数据
        $report = Db::name('crm_report')->where('create_staff_id','<>',$utid)->where($where)->select();

        foreach($report as &$v){

            //抄送
            $staff_id_arr = Db::name('report_cc_staff')->where('report_id',$v['report_id'])->column('staff_id');
            $final_staff = [];
            if(!empty($staff_id_arr)){
                foreach($staff_id_arr as $staff_id){
                    $tmp_staff = Db::name('crm_staff')->where('staff_id',$staff_id)->value('staff_name');
                    $final_staff[] = [
                        'staff_id' => $staff_id,
                        'staff_name' => $tmp_staff
                    ];
                }
            }

            $v['cc_staff'] = $final_staff;
            $department_id_arr = Db::name('report_cc_department')->where('report_id',$v['report_id'])->column('department_id');
            $final_department = [];
            if(!empty($department_id_arr)){
                foreach($department_id_arr as $department_id){
                    $tmp_department = Db::name('crm_department')->where('department_id',$department_id)->value('department_name');
                    $final_department[] = [
                        'department_id' => $department_id,
                        'department_name' => $tmp_department
                    ];
                }  
            }
            $v['cc_department'] = $final_department;

            //点赞
            $v['likes'] = Db::name('crm_report_likes')->field('staff_id,staff_name')->where('report_id',$v['report_id'])->count();
            //评论
            $v['comment'] = Db::name('crm_report_comment')->where('report_id',$v['report_id'])->count();
            //标签
            $v['type'] = Db::name('crm_report_type_relevance')->alias('a')
            ->join('crm_report_type b','a.type_id = b.type_id')
            ->where('a.report_id',$v['report_id'])
            ->column('b.title');

            //关联联系人
            $v['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
            ->join('sh_crm_staff b','a.staff_id = b.staff_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.staff_id,staff_name')
            ->select();
            
            //关联客户
            $v['client'] = Db::name('crm_report_client_relevance')->alias('a')
            ->join('sh_client b','a.client_id = b.client_id')
            ->where('a.report_id',$v['report_id'])
            ->field('b.client_id,client_name')
            ->select();

            //图片
            $v['image'] = Db::name('crm_report_img_relevance')->alias('a')
            ->join('sh_img b','a.img_id = b.img_id')
            ->where('a.report_id',$v['report_id'])->column('b.img_url');
            //附件
            $v['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
            ->join('sh_accessory b','a.accessory_id = b.accessory_id')
            ->where('a.report_id',$v['report_id'])->field('b.accessory_id,b.accessory_name,b.accessory_url')->select();

        }

        return $report;

    }

    //汇报详情
    public function report_detail($id){

        $utid = \request()->header('userId');

        $report = Db::name('crm_report')->where('report_id',$id)->find();

        //抄送
        $staff_id_arr = Db::name('report_cc_staff')->where('report_id',$report['report_id'])->column('staff_id');
        $final_staff = [];
        if(!empty($staff_id_arr)){
            foreach($staff_id_arr as $staff_id){
                $tmp_staff = Db::name('crm_staff')->where('staff_id',$staff_id)->value('staff_name');
                $final_staff[] = [
                    'staff_id' => $staff_id,
                    'staff_name' => $tmp_staff
                ];
            }
        }

        $report['cc_staff'] = $final_staff;
        $department_id_arr = Db::name('report_cc_department')->where('report_id',$report['report_id'])->column('department_id');
        $final_department = [];
        if(!empty($department_id_arr)){
            foreach($department_id_arr as $department_id){
                $tmp_department = Db::name('crm_department')->where('department_id',$department_id)->value('department_name');
                $final_department[] = [
                    'department_id' => $department_id,
                    'department_name' => $tmp_department
                ];
            }  
        }
        $report['cc_department'] = $final_department;

        //点赞
        $report['likes'] = Db::name('crm_report_likes')->field('staff_id,staff_name')->where('report_id',$report['report_id'])->select();

        $report['is_like'] = Db::name('crm_report_likes')->where('report_id',$report['report_id'])->where('staff_id',$utid)->count();
        
        //评论
        $report['comment'] = Db::name('crm_report_comment')->where('report_id',$report['report_id'])->where('parent_id',0)
        ->order('create_at asc')->select();
        foreach($report['comment'] as &$c){
            $c['is_show'] = false;
            $c['child_comment'] = Db::name('crm_report_comment')->order('create_at asc')->where('parent_id',$c['comment_id'])->select();
            foreach($c['child_comment'] as &$cc){
                $cc['is_show'] = false;
            }
        }
        //标签
        $report['type'] = Db::name('crm_report_type_relevance')->alias('a')
        ->join('crm_report_type b','a.type_id = b.type_id')
        ->where('a.report_id',$report['report_id'])
        ->column('b.title');

        //关联联系人
        $report['staff'] = Db::name('crm_report_staff_relevance')->alias('a')
        ->join('sh_crm_staff b','a.staff_id = b.staff_id')
        ->where('a.report_id',$report['report_id'])
        ->field('b.staff_id,staff_name')
        ->select();
        
        //关联客户
        $report['client'] = Db::name('crm_report_client_relevance')->alias('a')
        ->join('sh_client b','a.client_id = b.client_id')
        ->where('a.report_id',$report['report_id'])
        ->field('b.client_id,client_name')
        ->select();

        //图片
        $report['image'] = Db::name('crm_report_img_relevance')->alias('a')
        ->join('sh_img b','a.img_id = b.img_id')
        ->where('a.report_id',$report['report_id'])->column('b.img_url');
        //附件
        $report['accessory'] = Db::name('crm_report_accessory_relevance')->alias('a')
        ->join('sh_accessory b','a.accessory_id = b.accessory_id')
        ->where('a.report_id',$report['report_id'])->field('b.accessory_id,b.accessory_name,b.accessory_url')->select();

        return $report;

    }

}